﻿using AutoMapper;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using S.Common.Enums.Archives;
using S.Common.helper;
using S.EarTag.Domain.Dto;
using S.EarTag.Domain.Model;
using S.EarTag.Infrastructure.Repository.Interface.IEarTagRepository;
using S.EarTag.Read.API.Application.MediatorCommand.BreedingWeighCommand;

namespace S.EarTag.Read.API.Controllers
{
    /// <summary>
    /// 羊只称重
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class BreedingWeighController : ControllerBase
    {
        private readonly IMediator mediator;
        public BreedingWeighController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        #region 称重
        /// <summary>
        /// 称重
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public async Task<IActionResult> ListShow(BreedingWeighShowCommand t)
        {
            var result = await mediator.Send(t);
            var items = result.items;
            var totalCount = result.totalCount;
            var pageCount = result.pageCount;
            // 返回分页结果
            return Ok(new { items, totalCount, pageCount });
        }
        #endregion

        #region 枚举育种称重
        /// <summary>
        ///枚举育种称重
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public IActionResult WeighItemEnum()
        {
            List<object> list = new List<object>();
            foreach (var item in Enum.GetValues<WeighItem>())
            {
                list.Add(new
                {
                    name = item.ToString(),
                    value = item
                });
            }
            return Ok(list);
        }
        #endregion

        #region 枚举品种
        /// <summary>
        ///枚举品种
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public IActionResult VarietyEnumEnum()
        {
            List<object> list = new List<object>();
            foreach (var item in Enum.GetValues<VarietyEnum>())
            {
                list.Add(new
                {
                    name = item.ToString(),
                    value = item
                });
            }
            return Ok(list);
        }
        #endregion

        #region 耳号（种公种母后裔）
        /// <summary>
        /// 跨服务请求耳号
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public IActionResult GeteiTag(int id)
        {
            var jsonString = HttpClientHelper.GetResponse($"http://10.31.56.4:8011/api/Provide/SelMaleEmaleSel?id={id}");
            var response = JsonConvert.DeserializeObject<List<ESelectDto>>(jsonString);
            return Ok(response);
        }
        #endregion

        #region 类
        public class ESelectDto
        {
            /// <summary>
            /// 编号
            /// </summary>
            public string? value { get; set; }
            /// <summary>
            /// 名称
            /// </summary>
            public string? label { get; set; }
        }

        #endregion

    }
}
